﻿using OurMemory.Model.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;

namespace OurMemory
{
    public partial class FormMain : Form
    {
        private CustomMonthCalendar customMonthCalendar;
        private Timer timer;
        private Random random;
        public FormMain()
        {
            InitializeComponent();
              
            
            //设置背景
            panel2.BackgroundImageLayout = ImageLayout.Stretch;
            panel1.BackgroundImageLayout = ImageLayout.Stretch;
            panel3.BackgroundImageLayout = ImageLayout.Stretch;
            panel4.BackgroundImageLayout = ImageLayout.Stretch;


            this.ClientSize = new Size(1450, 800); // 设置客户区大小

            // 动态创建 CustomMonthCalendar
            customMonthCalendar = new CustomMonthCalendar();
            customMonthCalendar.Visible = false;
            customMonthCalendar.DateSelected += CustomMonthCalendar_DateSelected;
            this.Controls.Add(customMonthCalendar);

            InitializeBubbleComponents(); // 初始化泡泡组件
        }
        private void InitializeBubbleComponents()
        {
            // 初始化定时器
            timer = new Timer { Interval = 500 };
            timer.Tick += Timer_Tick;

            // 初始化随机数生成器
            random = new Random();
        }

        private void CreateBubble(Point location)
        {
            PictureBox bubble = new PictureBox
            {
                Size = new Size(10, 10),
                BackColor = Color.LightPink,
                Location = location,
                Visible = true
            };
            Controls.Add(bubble);
            bubble.BringToFront();

            // 动画效果
            Timer bubbleTimer = new Timer { Interval = 5 };
            bubbleTimer.Tag = bubble; // 将bubble关联到timer
            bubbleTimer.Tick += Bubble_TimerTick;
            bubbleTimer.Start();
        }

        private void Bubble_TimerTick(object sender, EventArgs e)
        {
            Timer bubbleTimer = (Timer)sender;
            PictureBox bubble = (PictureBox)bubbleTimer.Tag;

            if (bubble.Top < -bubble.Height)
            {
                Controls.Remove(bubble);
                bubbleTimer.Stop();
                bubble.Dispose();
            }
            else
            {
                bubble.Top -= 5;
            }
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            Point randomLocation = new Point(random.Next(this.ClientSize.Width), this.ClientSize.Height);
            CreateBubble(randomLocation);
        }

        private void CustomMonthCalendar_DateSelected(object sender, DateRangeEventArgs e)
        {
            // 处理用户选择日期后的逻辑
            MessageBox.Show($"您选择了: {e.Start.ToShortDateString()}");

            // 选择日期后隐藏 CustomMonthCalendar
            customMonthCalendar.Visible = false;
        }

        // （可选）监听窗体的 MouseClick 事件以隐藏 CustomMonthCalendar
        protected override void OnMouseClick(MouseEventArgs e)
        {
            base.OnMouseClick(e);

            // 如果点击的是非 CustomMonthCalendar 区域，则隐藏 CustomMonthCalendar
            if (customMonthCalendar.Visible && !customMonthCalendar.ClientRectangle.Contains(customMonthCalendar.PointToClient(Cursor.Position)))
            {
                customMonthCalendar.Visible = false;
            }
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            LOVE frm = new LOVE();
            var result = frm.ShowDialog();
            if (result != DialogResult.OK)
            {
                //非正常登录 
                this.Close();
            }
            else
            {
                this.label1.Text = CurrentUser.UserName;
                // 窗体加载完成后立即开始生成泡泡
                timer.Start();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (FormMainHM newForm = new FormMainHM())
            {
                newForm.ShowDialog();
            }
        }

        private void btnOF_Click(object sender, EventArgs e)
        {
            using (FormMainOF newForm = new FormMainOF())
            {
                newForm.ShowDialog();
            }

        }

        private void btnPS_Click(object sender, EventArgs e)
        {
            using (FormMainPS newForm = new FormMainPS())
            {
                newForm.ShowDialog();
            }

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            // 点击按钮时显示 CustomMonthCalendar
            customMonthCalendar.Visible = !customMonthCalendar.Visible;

            // 如果显示，则将其带到最前
            if (customMonthCalendar.Visible)
            {
                customMonthCalendar.BringToFront();
            }
        }

        private void pictureBox6_Click(object sender, EventArgs e)
        {

        }

        private void pictureBox5_Click(object sender, EventArgs e)
        {

        }
    }
}
